package gfx;

import javax.microedition.khronos.opengles.GL10;

import util.GameVars;

public class Background {
	private Image groundLayer;
	private Image overLayer;
	
	private float yTranslation;
	private float paralaxTranslation;
	
	private boolean isLoaded;
	
	public Background(){
		isLoaded = false;
	}
	
	public void loadGraphics(short stageID, GL10 gl){
		// Region Identification.  I can add embedded switches for special stages within the region (bosses, etc)
		switch(stageID / 100){
		case 0:
			Spritesheet.getInstance().loadBackgrounds(gl, stageID);
			break;
		default:
			break;
		}

		groundLayer = new Image(GameVars.TEXTURE_BACKGROUND_LOWER_LAYER, 6, 12);
		overLayer = new Image(GameVars.TEXTURE_BACKGROUND_UPPER_LAYER, 6, 12);
		
		isLoaded = true;		
	}
	
	public void update(long time){
		yTranslation += time * GameVars.TRANSLATION_MOD;
		if(yTranslation > 12)
			yTranslation -= 12;
		paralaxTranslation += time * GameVars.TRANSLATION_MOD * 1.2f;
		if(paralaxTranslation > 12)
			paralaxTranslation -= 12;
	}
	
	public void draw(GL10 gl){
		gl.glPushMatrix();
//		gl.glTranslatef(Camera.GL_WIDTH * .5f, Camera.GL_HEIGHT * .5f - yTranslation, 0);
		//										To get the seam at the top at the beginning
		gl.glTranslatef(Camera.GL_WIDTH * .5f, Camera.GL_HEIGHT - 6 - yTranslation, 0);
		
		groundLayer.draw(gl);
		
//		gl.glPushMatrix();
		gl.glTranslatef(0, 12, 0);
		
		groundLayer.draw(gl);
		
//		gl.glPopMatrix();
		
		gl.glPopMatrix();
		
		// Currently the shadow is drawn onto the overlayer
//		gl.glPushMatrix();
//		gl.glTranslatef(0, -1, 0);
//		gl.glColor4f(0, 0, 0, .25f);
//		overLayer.draw(gl);
//		gl.glPopMatrix();
		
		gl.glPushMatrix();
		gl.glTranslatef(Camera.GL_WIDTH * .5f, Camera.GL_HEIGHT - 6f - paralaxTranslation, 0);
		
		overLayer.draw(gl);
		
//		gl.glPushMatrix();
		gl.glTranslatef(0, 12f, 0);
		
		overLayer.draw(gl);
		
//		gl.glPopMatrix();
		
		gl.glPopMatrix();
	}
	
	public boolean isLoaded(){ return isLoaded; }
	public void reload(){ isLoaded = false; }
}
